<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        #box{
            width: 100px;
            height: 100px;
            border: 1px solid #000;
            background-color: #09f;
            position: absolute;
            left: 100px;
            top: 100px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <h1>JS 图层拖拽</h1>
    <hr>

    <div id="box"></div>

    <script>
        box.onmousedown = function(env) {
            this.innerHTML = '按下';
            this.style.backgroundColor = '#f90';
            // 鼠标按下的相对于box的位置
            var env = env || window.event;
            var left = env.offsetX;
            var top = env.offsetY;

            document.onmousemove = function(en) {
                var en = en || window.event;
                var x = en.x - left;
                var y = en.y - top;

                box.style.left = x + 'px';
                box.style.top = y + 'px';
            }
        }

        document.onmouseup = function() {
            box.innerHTML = '松开';
            box.style.backgroundColor = '#09f';
            document.onmousemove = null;
        }
    </script>
</body>
</html>